package com.zhongwang.mms.module.half.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.model.HInventoryModel;
import com.zhongwang.mms.module.half.model.HalfInveAdjModel;
import com.zhongwang.mms.module.half.service.HalfInveAdjService;
import com.zhongwang.mms.module.system.log.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zhongwang.mms.common.JsonResult.fail;

/**
 *成品库库存调整
 */
@Controller
@RequestMapping("/half/halInvManage/halfInveAdjApply")
public class HalfInveAdjController extends BaseController {
    @Autowired
    private HalfInveAdjService halfInveAdjService;
    @Autowired
    private LogService logService;
    @Autowired
    private HInventoryDao hInventoryDao;

    /**
     * 库存调整页面
     * @return 页面
     */
    @GetMapping
    @RequiresPermissions({"halinv:haladjlist:view"})
    public String halfInveAdjPage(Model model) {
        model.addAttribute("whiIds", halfInveAdjService.findWhiId());//查询仓库列表
        return "half/halInvManage/halfInveAdjApply/halfInvAdj";
    }

    /**
     * 库存调整列表
     * @param hAdjustmentApplyDetail 调整列表查询条件
     * @return 库存调整list
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"halinv:haladjlist:view"})
    public JsonResult HalfInveAdjlist(HalfInveAdjModel hAdjustmentApplyDetail) {
        startPage();
        List<HalfInveAdjModel> list = halfInveAdjService.findAdjList(hAdjustmentApplyDetail);
        return jsonResult(list);
    }

    /**
     * 添加盘点明细页面
     * 在调整列表页面点击添加按钮，跳转到查询库存页面
     * @return 页面
     */
    @GetMapping("/invList")
//    @RequiresPermissions({"halinv:haladjlist:view"})
    public String invePage(Model model) {
        model.addAttribute("whiIds", halfInveAdjService.findWhiId());
        return "half/halInvManage/halfInveAdjApply/invList";
    }

    /**
     * 库存查询列表
     * @param hInventory 库存列表查询条件
     * @return list 库存列表
     */
    @ResponseBody
    @PostMapping("/invList")
    @RequiresPermissions({"halinv:haladjlist:view"})
    public JsonResult Invelist(HInventoryModel hInventory) {
        startPage();
        List<HInventoryModel> list = halfInveAdjService.findInveList(hInventory);
        return jsonResult(list);
    }

    /**
     * 根据id查找库存
     * 点击每行后的调整按钮，弹出库存信息模态框
     * @return 库存信息
     */
    @ResponseBody
    @PostMapping("/invAdj")
    @RequiresPermissions({"halinv:haladjlist:edit"})
    public HInventoryModel findInve(Integer id) {
        return halfInveAdjService.findHInventoryById(id);
    }


    /**
     * 保存库存调整
     * 从列表内选择的库存进行调整，在模态框内修改库存信息后，将调整信息存入调整表，生成调整单
     * @param hInventory 前端返回表格内的库存信息
     * @return 保存是否成功消息
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"halinv:haladjlist:edit"})
    public JsonResult halInvSave(HInventoryModel hInventory) {
        int row = halfInveAdjService.halInvSave(hInventory);
        if(row == 0){
            return fail("此条库存已有未提交的库存申请！");
        }else if (row == -1){
            return  fail("未做修改!");
        }else return success();
    }

    /**
     * 打开修改库存模态框
     * 调整单未提交待审前，可修改调整单信息。按id查找调整单信息，返回模态框
     * @param id 调整单id
     * @return 调整单信息
     */
    @ResponseBody
    @PostMapping("/editOpen")
    @RequiresPermissions({"halinv:haladjlist:edit"})
    public HalfInveAdjModel findInvAdj(Integer id){
        return halfInveAdjService.findInvAdjById(id);
    }

    /**
     * 保存修改库存调整
     * 调整单为未提交状态时，修改调整单，后保存，只修改调整单的After在库数量
     * @param hAdjustmentApplyDetail 前端返回的调整单信息
     * @return 修改是否成功信息
     */
    @ResponseBody
    @PostMapping("/editSave")
    @RequiresPermissions({"halinv:haladjlist:edit"})
    public JsonResult halInvEditSave(HalfInveAdjModel hAdjustmentApplyDetail) {
        int row = halfInveAdjService.halInvEditSave(hAdjustmentApplyDetail);
        if(row == 0){
            return fail("修改物料请删除本条调整，重新添加调整！");
        }else if (row == -1){
            return  fail("未做修改!");
        }else return success();
    }

    /**
     * 删除库存调整
     * 生成的调整单未提交前，可进行删除。
     * @param hAdjustmentApplyDetail 前端返回的调整单信息
     * @return 删除成功结果
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"halinv:haladjlist:edit"})
    public JsonResult delAjust(HalfInveAdjModel hAdjustmentApplyDetail)
    {
        HalfInveAdjModel h = halfInveAdjService.findInvAdjById(hAdjustmentApplyDetail.getAadId());
        logService.saveLog("成品库：删除库存调整", h.getAadAfterMatName(),h.getAadAfterNum());
        return jsonResult(halfInveAdjService.halInvDel(hAdjustmentApplyDetail));
    }

    /**
     * 批量提交库存调整
     * 调整审核页面返回调整单id字符串
     * @param arr 调整单id字符串
     * @return 提交成功结果
     */
    @ResponseBody
    @PostMapping("/submit")
    @RequiresPermissions({"halinv:haladjlist:edit"})
    public JsonResult submitAjust(String arr) {
        List<String> list = Arrays.asList(arr.split(","));
        return jsonResult(halfInveAdjService.submitAjust(list)) ;
    }

    /**
     * 查看已提交库存调整
     * 已提交的库存调整，点击模态框查看调整信息
     * @param id 前端返回的调整单id
     * @return 调整单信息
     */
    @ResponseBody
    @PostMapping("/viewAdj")
    @RequiresPermissions({"halinv:haladjlist:view"})
    public HalfInveAdjModel findInvViewAdj(Integer id){
        return halfInveAdjService.viewInvAdjById(id);
    }

    /**
     * 保存新增库存调整
     * 如果所要调整的物料，库存内不存在，则需要新增库存调整，后生成调整单
     * @param hAdjustmentApplyDetail 前端返回的表格内填写的库存调整信息
     * @return 保存成功结果
     */
    @ResponseBody
    @PostMapping("/createSave")
    @RequiresPermissions({"halinv:haladjlist:edit"})
    public JsonResult halInvCreateSave(HalfInveAdjModel hAdjustmentApplyDetail) {
        logService.saveLog("成品库：保存新增库存调整", hAdjustmentApplyDetail.getAadAfterMatName(),hAdjustmentApplyDetail.getAadAfterNum());
        return jsonResult(halfInveAdjService.halInvCreateSave(hAdjustmentApplyDetail));
    }

    /**
     * 项目编码存在校验
     * 页面级校验
     * @param param
     * @return
     */
    @PostMapping("/codeExist")
    @ResponseBody
    public String codeExist(HalfInveAdjModel param) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", halfInveAdjService.codeExist(param));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }
}


